/**
 * @作者 7七月
 * @微信公号 林间有风
 * @开源项目 $ http://talelin.com
 * @免费专栏 $ http://course.talelin.com
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2020-02-26 23:07
 */
package com.ruoyi.cat.utils;



import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

public class CommonUtil {

    public static Calendar addSomeSeconds(Calendar calendar, int seconds) {
        calendar.add(Calendar.SECOND, seconds);
        return calendar;
    }

    public static String timestamp10(){
        Long timestamp13 = Calendar.getInstance().getTimeInMillis();
        String timestamp13Str = timestamp13.toString();
        return timestamp13Str.substring(0, timestamp13Str.length() - 3);
    }

    public static Boolean isInTimeLine(Date date, Date start, Date end) {
        Long time = date.getTime();
        Long startTime = start.getTime();
        Long endTime = end.getTime();
        if (time > startTime && time < endTime) {
            return true;
        }
        return false;
    }

    //period 单位：秒
    public static Boolean isOutOfDate(Date startTime, Long period) {
        Long now = Calendar.getInstance().getTimeInMillis();
        Long startTimeStamp = startTime.getTime();
        Long periodMillSecond = period * 1000;
        if (now > (startTimeStamp + periodMillSecond)) {
            return true;
        }
        return false;
    }

    public static Boolean isOutOfDate(Date expiredTime) {
        Long now = Calendar.getInstance().getTimeInMillis();
        Long expiredTimeStamp = expiredTime.getTime();
        if(now > expiredTimeStamp){
            return true;
        }
        return false;
    }

    public static String yuanToFenPlainString(BigDecimal p){
        p = p.multiply(new BigDecimal("100"));
        return CommonUtil.toPlain(p);
    }

    public static BigDecimal applyDiscount(BigDecimal chineseMedicine, String discountStr) {
        // 去掉折扣字符串中的百分比符号
        String cleanDiscountStr = discountStr.replace("%", "");

        // 检查是否是有效的数字格式
        if (!isNumeric(cleanDiscountStr)) {
            throw new IllegalArgumentException("Invalid discount format: " + discountStr);
        }

        // 将字符串转换为BigDecimal
        BigDecimal discount = new BigDecimal(cleanDiscountStr);

        // 将折扣从百分比转换为小数
        BigDecimal discountFactor = discount.divide(BigDecimal.valueOf(100));

        // 计算折扣金额
        BigDecimal discountAmount = chineseMedicine.multiply(discountFactor);

        // 计算打折后的价格
        BigDecimal discountedPrice = chineseMedicine.subtract(discountAmount);

        return discountedPrice;
    }

    // 辅助方法：检查字符串是否是有效的数字
    private static boolean isNumeric(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static BigDecimal calculateDiscountAmount(BigDecimal chineseMedicine, String discountStr) {
        // 去掉折扣字符串中的百分比符号
        String cleanDiscountStr = discountStr.replace("%", "");

        // 检查是否是有效的数字格式
        if (!isNumeric(cleanDiscountStr)) {
            throw new IllegalArgumentException("Invalid discount format: " + discountStr);
        }

        // 将字符串转换为BigDecimal
        BigDecimal discount = new BigDecimal(cleanDiscountStr);

        // 将折扣从百分比转换为小数
        BigDecimal discountFactor = discount.divide(BigDecimal.valueOf(100));

        // 计算折扣金额
        BigDecimal discountAmount = chineseMedicine.multiply(discountFactor);

        return discountAmount;
    }

    public static boolean isGreaterThanOne(BigDecimal value) {
        // 1.00的BigDecimal表示
        BigDecimal one = new BigDecimal("1.00");

        // 使用compareTo方法比较，value > 1 返回1，value = 1 返回0，value < 1 返回-1
        return value.compareTo(one) > 0;
    }

    public static String toPlain(BigDecimal p){
        return p.stripTrailingZeros().toPlainString();
    }
}
